import axios from "axios"
import { checkStatus } from "./utils"
//  请求头拦截
axios.interceptors.request.use(config => {
    //  获取token
    const token = window.localStorage.getItem("token");
    const headers = {};
    //  如果有token 设置 请求头
    if (token) {
        headers.authorization = "Bearer " + token;
    }
    return {
        ...config,
        ...headers
    }
})

// 响应头拦截器
axios.interceptors.response.use(function (response) {
    return Promise.resolve(checkStatus(response));
}, function (error) {
    // 对响应错误做处理...
    if (error.response) {
        return Promise.reject(checkStatus(error.response));
    } else if (
        error.code == "ECONNABORTED" &&
        error.message.indexOf("timeout") != -1
    ) {
        return Promise.reject({ msg: "请求超时" });
    } else {
        return Promise.reject({});
    }
});




export default axios;